<template>
  <div>
    <el-row style="display:flex;justify-content:flex-start; margin-bottom:20px;">
      <!-- <el-input placeholder="人员姓名" style="width:15%"> -->
      <el-input
        v-model="laborData.hash.username"
        placeholder="人员姓名"
        style="width:15%;margin-left:25px"
        clearable
      ></el-input>
      <!-- </el-input> -->
      <el-input
        placeholder="合同号"
        style="width:15%;margin-left:25px"
        v-model="laborData.hash.cnumber"
        clearable
      ></el-input>
      <el-select
        v-model="laborData.hash.ctype"
        placeholder="请选择合同类型"
        style="width:15%;margin-left:25px"
        clearable
      >
        <el-option
          v-for="item in itemdata"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>

      <el-date-picker
        v-model="begintime"
        style="margin-left:25px;"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="合同开始日期"
        endPlaceholder="合同开始日期"
        valueFormat="yyyy-MM-dd"
      ></el-date-picker>
      <el-date-picker
        v-model="endtime"
        style="margin:0 25px;"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="合同结束日期"
        endPlaceholder="合同结束日期"
        valueFormat="yyyy-MM-dd"
      ></el-date-picker>
      <el-button size="mini" type="primary" @click="getlaborlist">搜索</el-button>
    </el-row>

    <el-row>
      <el-button size="mini" @click="showaddlabor">添加合同</el-button>
      <el-button size="mini" type="primary" @click="amandlabor">编辑</el-button>
      <el-button size="mini" type="success" @click="deletelabor">删除</el-button>
    </el-row>
    <!-- {{this.itemval}} -->
    <el-divider></el-divider>
    <labortable
      :tableData="laborlist"
      @itemvals="itemvals"
      :pagobj.sync="pagobj"
      :ischeck="false"
      id="tableExcel"
    ></labortable>
    <addlabor
      :itemdata="itemdata"
      v-if="isaddlabor"
      :itemvalname="itemvalname"
      :chousebtn="btnvalue"
    ></addlabor>
  </div>
</template>

<script>
import addlabor from '~/components/person/laborbargin/addlabor.vue';
import labortable from '~/components/person/laborbargin/labortable.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      dialogVisible: false,
      ts: this.newStore(),
      laborData: {
        cols: [],
        hash: {
          username: '',
          cnumber: '',
          begintime: [],
          endtime: [],
          ctype: '',
          pid: '',
          id: '',
        },
      },
      pagesize: 0,
      currentPage: 0,
      pagobj: {
        // 分页
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      laboridarr: [], // 删除所用的数组id
      isaddlabor: false, // 添加弹窗
      // isaddlabor2: false, //编辑弹窗
      btnvalue: 0,
      Object: '',
      itemval: {}, // 编辑传回来的d对象
      cnumber: '',
      laborlist: [],
      itemdata: [], // 项目类型
      begintime: [],
      endtime: [],
      // changelist: {}
    };
  },
  components: {
    addlabor,
    labortable,
  },

  // watch: {
  //   // Object() {
  //   //   this.getlaborlist();
  //   // },
  //   changelist() {
  //     this.getlaborlist();
  //   }
  // },
  created() {
    this.getlaborlist();
    this.getitem();
  },

  methods: {
    // 删除劳动合同
    deletelabor() {
      // console.log(this.laboridarr, "0");
      if (this.laboridarr.length > 1) {
        this.$confirm('是否批量删除劳动合同信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '09D3B9E20F101E11DEFE3350091AFF9B724AECA2C1F5AF24491E85DD9F1F0F47',
            cnumber: this.laboridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getlaborlist();
            }
          });
        });
      } else if (this.laboridarr.length == 1) {
        this.$confirm('是否删除该劳动合同信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '09D3B9E20F101E11DEFE3350091AFF9B724AECA2C1F5AF24491E85DD9F1F0F47',
            cnumber: this.laboridarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getlaborlist();
            }
          });
        });
      } else {
        this.$alert('请选择劳动合同', '提示');
      }
    },
    // 编辑劳动合同
    amandlabor() {
      (this.dialogVisible = true), (this.btnvalue = 2);
      if (Object.keys(this.itemval).length == 1) {
        let itemvals = {};
        itemvals = this.itemval[0];
        this.itemvalname = itemvals;
        // console.log(this.itemvalname,'0123')
        this.reload('isaddlabor');
      } else {
        this.$alert('编辑只能编辑一个', '提示');
      }
    },
    // table 传给父组件
    itemvals(val) {
      // console.log(val, "wwwwww");
      this.itemval = val;
      this.laboridarr = [];
      for (let i = 0; i < this.itemval.length; i++) {
        this.laboridarr.push(this.itemval[i].hash.cnumber);
        // this.laboridarr.push(this.itemval[i].hash.id);
      }
      // console.log(this.laboridarr, "23");
    },
    // 监听子组件的(添加)
    addlabor(val) {
      this.Object = {};
      this.Object = val;
      // val.hash.holder = val.hash.holders;
      // console.log(val, "22223322");
      // this.carlist.unshift(val);
      // console.log(this.laborlist, "66666");
    },
    // 公共组件刷新
    reload(partsname) {
      console.log(partsname);
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击添加劳动合同信息
    showaddlabor() {
      (this.dialogVisible = true), (this.btnvalue = 1);
      this.reload('isaddlabor');
    },

    // 分页
    handleCurrentChange() {
      this.getlaborlist();
    },
    // 获取项目类型
    getitem() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0203',
        codetype: '02',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.itemdata = ts.d.data.operation1;
          // console.log(this.itemdata,'888')
        }
      });
    },
    // 获取列表
    getlaborlist() {
      // console.log("2222");
      if (this.begintime == null) {
        this.begintime = [];
      }
      if (this.endtime == null) {
        this.endtime = [];
      }
      const req = {
        handle:
          '09D3B9E20F101E11DEFE3350091AFF9B724AECA2C1F5AF24BF35823F59EB452A',
        username: this.laborData.hash.username,
        cnumber: this.laborData.hash.cnumber,
        begintime: this.begintime[0],
        begintimetwo: this.begintime[1],
        endtime: this.endtime[0],
        endtimetwo: this.endtime[1],
        ctype: this.laborData.hash.ctype,
        id: this.laborData.hash.id,
        pid: this.laborData.hash.pid,
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.laborlist = ts.d.data.operation1;
        console.log(req)
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
        // console.log(this.laborlist,'789')
      });
    }
  },
};
</script>

